package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.codec.Utf8;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ author     ：廖传港
 * @ date       ：Created in 2021/1/15 19:11
 * @ description：首页
 * @ modified By：
 * @ ersion:
 */
@Api("首页")
@Controller
public class IndexController {


    @Autowired
    private ProductFeignClient productFeignClient;

    @ApiOperation("获取首页的三级分类数据")
    @GetMapping("/")
    public String index(Model model) {
        List<Map<String, Object>> resultMapList = getData();
        model.addAttribute("list", resultMapList);
        return "index/index";
    }



//    @ApiOperation("获取首页的三级分类数据")
//    @GetMapping("/")
//    public String index() {
//        return "index";
//    }




    @Autowired
    private TemplateEngine templateEngine;

    //生成静态化页面儿
    @GetMapping("/createHtml")
    @ResponseBody //因为要返回对象，所以要加这个注解
    public Result createHtml() throws IOException {
        //1：模板+数据==输出
        //模板 页面 （标签：thymeleaf）
        //数据 一二三级分类
        Context comtext = new Context();
        comtext.setVariable("List",getData());
        //输出 D：//tem
//        Writer writer = new OutputStreamWriter(new FileOutputStream("D:\\temp\\index.html"), "utf-8");
        //也可以这么写
        Writer writer = new PrintWriter("D:\\temp\\index.html", "utf-8");

        templateEngine.process("index/index", comtext,writer);
        System.out.println("页面儿已经生成");

        return Result.ok();
    }


    public List<Map<String, Object>> getData() {
        //分类视图集合
        List<BaseCategoryView> baseCategoryViewList = productFeignClient.getBaseCategoryViewList();

        //key:getCategory1Id  ,valu:List<BaseCategoryView>
        Map<Long, List<BaseCategoryView>> baseCategory1ListMap = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

        int index = 1;

        //最终结果对象
        List<Map<String, Object>> resultMapList = new ArrayList<>();
        for (Map.Entry<Long, List<BaseCategoryView>> baseCategory1Entry : baseCategory1ListMap.entrySet()) {
            Map<String, Object> baseCategory1Map = new HashMap<>();
            List<BaseCategoryView> getCategory1value = baseCategory1Entry.getValue();
            baseCategory1Map.put("index", index++);
            baseCategory1Map.put("categoryId", baseCategory1Entry.getKey());
            baseCategory1Map.put("categoryName", getCategory1value.get(0).getCategory1Name());
            //子分类集合 二级分类数据
            Map<Long, List<BaseCategoryView>> Category1value = getCategory1value.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            List<Map<String, Object>> baseCategory2MapList = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> baseCategory2Entry : Category1value.entrySet()) {
                Map<String, Object> baseCategory2Map = new HashMap<>();
                List<BaseCategoryView> baseCategoryViews = baseCategory2Entry.getValue();
                baseCategory2Map.put("categoryId", baseCategory2Entry.getKey());
                baseCategory2Map.put("categoryName", baseCategoryViews.get(0).getCategory2Name());

                //子分类集合，三级
                List<Map> mapList = baseCategoryViews.stream().map(baseCategoryView -> {
                    Map map = new HashMap();
                    map.put("categoryId", baseCategoryView.getCategory3Id());
                    map.put("categoryName", baseCategoryView.getCategory3Name());
                    return map;
                }).collect(Collectors.toList());
                baseCategory2Map.put("categoryChild", mapList);
                baseCategory2MapList.add(baseCategory2Map);
            }

            baseCategory1Map.put("categoryChild", baseCategory2MapList);
            resultMapList.add(baseCategory1Map);

        }
        return resultMapList;
    }


}
